﻿@using Smartstore.Web.Models.Cart;
@using Smartstore.Core.Catalog.Products;
@using Smartstore.Core.Checkout.Cart;
@using Smartstore.Core.Common;

@model ShoppingCartModel

@{
    @foreach (var item in Model.Items)
    {
        if (item.ProductType == ProductType.BundledProduct)
        {
            await SimpleProduct(item);
            await BundleProducts(item);
        }
        else
        {
            await SimpleProduct(item);
        }
    }

    async Task SimpleProduct(ShoppingCartModel.ShoppingCartItemModel item)
    {
        var deliveryPresentation = Model.DeliveryTimesPresentation;

        <div class="cart-row">
            <div class="cart-col cart-col-main">
                <div class="row sm-gutters">

                    <div sm-if="Model.ShowProductImages && item.Image != null && item.Image.HasImage()" class="col cart-item-img">
                        <img sm-model="item.Image" class="img-fluid" />
                    </div>

                    <div class="col">
                        <a class="cart-item-link" href="@item.ProductUrl" title="@T("Products.Details")" sm-language-attributes-for="item.ProductName">@item.ProductName</a>

                        @if (Model.DisplayShortDesc && item.ShortDesc.HasValue())
                        {
                            <div class="cart-item-desc fs-sm" sm-language-attributes-for="item.ShortDesc">
                                @Html.Raw(item.ShortDesc.Value.Truncate(250, "..."))
                            </div>
                        }

                        <div sm-if="Model.DisplayBasePrice && item.BasePrice.HasValue()" class="cart-item-base-price text-muted fs-sm lrm">
                            @item.BasePrice
                        </div>

                        <div class="cart-item-attrs fs-sm my-2">
                            @if (deliveryPresentation != DeliveryTimesPresentation.None && item.IsShipEnabled)
                            {
                                var showDeliveryName =
                                    item.DeliveryTimeName.HasValue() && item.DeliveryTimeHexValue.HasValue() &&
                                    (deliveryPresentation == DeliveryTimesPresentation.LabelOnly ||
                                    deliveryPresentation == DeliveryTimesPresentation.LabelAndDate ||
                                    !item.DeliveryTimeDate.HasValue());

                                var showDeliveryDate =
                                    (deliveryPresentation == DeliveryTimesPresentation.DateOnly || deliveryPresentation == DeliveryTimesPresentation.LabelAndDate) &&
                                    item.DeliveryTimeDate.HasValue();

                                <div sm-if="showDeliveryName" class="row cart-item-attr deliverytime-group">
                                    <div class="col-3">@T("Products.DeliveryTime")</div>
                                    <div class="col-9">
                                        <span class="delivery-time pl-0" style="color:@item.DeliveryTimeHexValue" title="@item.DeliveryTimeName">
                                            <i class="fa fa-circle delivery-time-status"></i>
                                            <span sm-language-attributes-for="item.DeliveryTimeName">@item.DeliveryTimeName</span>
                                        </span>
                                    </div>
                                </div>

                                <div sm-if="showDeliveryDate" class="row cart-item-attr deliverytime-group">
                                    <div class="col-3">
                                        @Html.Raw(!showDeliveryName ? T("Products.DeliveryDate").Value : "&nbsp;")
                                    </div>
                                    <div class="col-9">
                                        <span class="delivery-date">@Html.Raw(item.DeliveryTimeDate)</span>
                                    </div>
                                </div>
                            }

                            <div sm-if="Model.ShowSku && item.Sku.HasValue()" class="row cart-item-attr text-nowrap">
                                <div class="col-3">@T("ShoppingCart.SKU"):</div>
                                <div class="col-9">@item.Sku</div>
                            </div>

                            <div sm-if="Model.DisplayWeight && item.Weight > 0" class="row cart-item-attr text-nowrap">
                                <div class="col-3">@T("ShoppingCart.Weight"):</div>
                                <div class="col-9">@((item.Weight * item.EnteredQuantity).ToString("N2")) @Model.MeasureUnitName</div>
                            </div>

                        </div>

                        <div sm-if="item.AttributeInfo.HasValue()" class="attributes text-muted fs-sm mb-2">
                            @Html.Raw(item.AttributeInfo)
                        </div>

                        <div sm-if="item.RecurringInfo.HasValue()" class="recurring-info">
                            @Html.Raw(item.RecurringInfo)
                        </div>

                        <div sm-if="!Model.IsEditable && ((item.IsDownload && item.HasUserAgreement) || (Model.DisplayEsdRevocationWaiverBox && item.IsEsd))" class="alert alert-warning mb-0">

                            <div sm-if="item.IsDownload && item.HasUserAgreement" class="download-agreement form-check">
                                <input type="checkbox" id="AgreeUserAgreement@(item.ProductId)" name="AgreeUserAgreement@(item.ProductId)" class="form-check-input" />
                                <label class="form-check-label" for="AgreeUserAgreement@(item.ProductId)">
                                    <span>@T("DownloadableProducts.UserAgreementConfirmation", item.ProductId)</span>
                                </label>
                            </div>

                            <div sm-if="Model.DisplayEsdRevocationWaiverBox && item.IsEsd" class="esd-revocation-waiver form-check">
                                <input type="checkbox" id="AgreeEsdRevocationWaiver@(item.ProductId)" name="AgreeEsdRevocationWaiver@(item.ProductId)" class="form-check-input" />
                                <label class="form-check-label" for="AgreeEsdRevocationWaiver@(item.ProductId)">
                                    <span>@T("Checkout.EsdRevocationWaiverConfirmation")</span>
                                </label>
                            </div>

                        </div>

                        <div sm-if="item.Warnings.Any()" class="alert alert-danger">
                            @foreach (var warning in item.Warnings)
                            {
                                <p>@warning</p>
                            }
                        </div>
                        
                        <zone name="cartitem_desc_after" model="item.Id"></zone>
                    </div>

                    <div sm-if="Model.IsEditable" class="col col-auto">
                        <div class="cart-row-actions btn-group-vertical">
                            <a class="btn btn-gray btn-to-danger btn-sm btn-icon ajax-action-link" title="@T("Common.Remove")"
                               href="#"
                               rel="nofollow"
                               data-href='@Url.Action("DeleteCartItem", "ShoppingCart", new { cartItemId = item.Id })'
                               data-name="@item.ProductName"
                               data-type="cart"
                               data-id="@item.Id"
                               data-action="remove">
                                <i class="fa fa-2x">×</i>
                            </a>
                            @if (Model.DisplayMoveToWishlistButton && !item.DisableWishlistButton)
                            {
                                <a class="btn btn-secondary btn-sm btn-icon ajax-action-link" title="@T("ShoppingCart.MoveToWishlist")"
                                   href="#"
                                   rel="nofollow"
                                   data-href='@Url.Action("MoveItemBetweenCartAndWishlist", "ShoppingCart", new { cartItemId = item.Id, cartType = ShoppingCartType.ShoppingCart, isCartPage = true })'
                                   data-name="@item.ProductName"
                                   data-type="wishlist"
                                   data-action="addfromcart">
                                    <i class="fal fa-heart"></i>
                                </a>
                            }
                        </div>
                    </div>
                </div>
            </div>

            <div class="cart-col cart-col-price" data-caption="@T("Order.Product(s).Price")">
                <span class="price">@item.UnitPrice</span>
            </div>

            <div class="cart-col cart-col-qty" data-caption="@T("Order.Product(s).Quantity")">
                @if (Model.IsEditable)
                {
                    @Html.EditorFor(x => item, "QtyInput", new
                    {
                        htmlAttributes = new
                        {
                            data_href= Url.Action("UpdateCartItem", "ShoppingCart", new { sciItemId = item.Id, isCartPage = true }),
                            data_sci_item = item.Id,
                            id = $"itemquantity{item.Id}",
                            Name = $"itemquantity{item.Id}"
                        }
                    })
                }
                else
                {
                    <span>@item.EnteredQuantity</span>
                }
            </div>

            <div class="cart-col cart-col-price cart-col-subtotal" data-caption="@T("Order.Product(s).Total")">
                <span class="price">@item.SubTotal</span>

                <div sm-if="item.Discount > decimal.Zero" class="discount fs-sm text-muted pt-1">
                    <div>@T("ShoppingCart.ItemYouSave"):</div>
                    <div>@item.Discount</div>
                </div>
            </div>
        </div>
    }

    async Task BundleProducts(ShoppingCartModel.ShoppingCartItemModel parentItem)
    {
        foreach (var item in parentItem.ChildItems.OrderBy(x => x.BundleItem.DisplayOrder))
        {
            <div class="cart-row cart-row-child">
                <div class="cart-col cart-col-main">
                    <div class="row sm-gutters">
                        <div class="col cart-item-img">
                            <!-- Spacer -->
                        </div>
                        <div class="col">
                            <div class="d-flex w-100 align-items-center justify-content-start flex-nowrap">
                                @if (Model.ShowProductBundleImages)
                                {
                                    <div class="mr-3" style="width:@Math.Min(Model.BundleThumbSize, 60)px;">
                                        @if (item.Image != null && item.Image.HasImage() && !item.BundleItem.HideThumbnail)
                                        {
                                            <img sm-model="item.Image" class="img-fluid" />
                                        }
                                    </div>
                                }
                                <div>
                                    
                                    <span sm-if="item.EnteredQuantity > 1 && item.BundlePerItemShoppingCart" class="font-weight-medium">
                                        @(item.EnteredQuantity)&nbsp;&#215;
                                    </span>
                                    
                                    @if (item.VisibleIndividually)
                                    {
                                        <a class="cart-item-link" href="@item.ProductUrl" title="@T("Products.Details")" sm-language-attributes-for="item.ProductName">
                                            @item.ProductName
                                        </a>
                                    }
                                    else
                                    {
                                        <span class="cart-item-link" sm-language-attributes-for="item.ProductName">@item.ProductName</span>
                                    }
                                </div>
                            </div>

                            @if (Model.DisplayShortDesc && item.ShortDesc.HasValue())
                            {
                                <div class="cart-item-desc mt-2" sm-language-attributes-for="item.ShortDesc">
                                    @Html.Raw(item.ShortDesc.Value.Truncate(210, "..."))
                                </div>
                            }

                            <div sm-if="Model.DisplayBasePrice && item.BasePrice.HasValue()" class="cart-item-base-price text-muted mt-2 lrm">
                                @item.BasePrice
                            </div>

                            <div sm-if="item.AttributeInfo.HasValue()" class="cart-item-attrs text-muted mt-2">
                                @Html.Raw(item.AttributeInfo)
                            </div>
                            
                            <div sm-if="item.Warnings.Any()" class="alert alert-danger mt-2 mb-0">
                                @foreach (var warning in item.Warnings)
                                {
                                    <p>@warning</p>
                                }
                            </div>
                        </div>
                    </div>
                </div>
                <div class="cart-col cart-col-price">
                    @Html.Raw(item.BundleItem.PriceWithDiscount.HasValue() ? item.BundleItem.PriceWithDiscount : "&nbsp;")
                </div>
                <div class="cart-col">&nbsp;</div>
                <div class="cart-col">&nbsp;</div>
            </div>
        }
    }
}